<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="Runtime先了解一下ISA指针(方案1位运算)在arm64架构之前，isa就是一个普通的指针，存储着Class、Meta-Class对象的内存地址  从arm64架构开始，对isa进行了优化，变成了一个共用体（union）结构，还使用位域来存储更多的信息">
<meta property="og:type" content="article">
<meta property="og:title" content="runtime">
<meta property="og:url" content="http://yoursite.com/2018/06/19/runtime/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="Runtime先了解一下ISA指针(方案1位运算)在arm64架构之前，isa就是一个普通的指针，存储着Class、Meta-Class对象的内存地址  从arm64架构开始，对isa进行了优化，变成了一个共用体（union）结构，还使用位域来存储更多的信息">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1ftitgki5z6j30vw0eowha.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1ftitovgoupj308x07d74o.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1ftitovgoupj308x07d74o.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tKfTcly1ftixime754j30sn0dddjj.jpg">
<meta property="og:updated_time" content="2019-07-19T05:40:06.081Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="runtime">
<meta name="twitter:description" content="Runtime先了解一下ISA指针(方案1位运算)在arm64架构之前，isa就是一个普通的指针，存储着Class、Meta-Class对象的内存地址  从arm64架构开始，对isa进行了优化，变成了一个共用体（union）结构，还使用位域来存储更多的信息">
<meta name="twitter:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1ftitgki5z6j30vw0eowha.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/06/19/runtime/"/>





  <title>runtime | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/06/19/runtime/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">runtime</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-06-19T17:47:47+08:00">
                2018-06-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/InterView/" itemprop="url" rel="index">
                    <span itemprop="name">InterView</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/06/19/runtime/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/06/19/runtime/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="Runtime"><a href="#Runtime" class="headerlink" title="Runtime"></a>Runtime</h4><h5 id="先了解一下ISA指针-方案1位运算"><a href="#先了解一下ISA指针-方案1位运算" class="headerlink" title="先了解一下ISA指针(方案1位运算)"></a>先了解一下ISA指针(方案1位运算)</h5><p><strong>在arm64架构之前，isa就是一个普通的指针，存储着Class、Meta-Class对象的内存地址</strong> </p>
<p><strong>从arm64架构开始，对isa进行了优化，变成了一个共用体（union）结构，还使用位域来存储更多的信息</strong></p>
<a id="more"></a>
<p><strong>感谢MJ老师细心讲解</strong> </p>
<p><img src="https://ws2.sinaimg.cn/large/006tKfTcly1ftitgki5z6j30vw0eowha.jpg" alt=""></p>
<p><img src="https://ws2.sinaimg.cn/large/006tKfTcly1ftitovgoupj308x07d74o.jpg" alt=""></p>
<p><strong>了解一下位运算</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//	创建一个类</span><br><span class="line">MJPerson</span><br><span class="line">@property (assign, nonatomic, getter=isTall) BOOL tall;</span><br><span class="line">@property (assign, nonatomic, getter=isRich) BOOL rich;</span><br><span class="line">@property (assign, nonatomic, getter=isHansome) BOOL handsome;</span><br><span class="line">// 这三个属性每一个占据1个字节内存,接下来要实现的就是用一个字节保存三个数据,因都是1或者0</span><br></pre></td></tr></table></figure>
<p><strong>首先在接口中定义getter,setter</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">- (void)setTall:(BOOL)tall;</span><br><span class="line">- (void)setRich:(BOOL)rich;</span><br><span class="line">- (void)setHandsome:(BOOL)handsome;</span><br><span class="line"></span><br><span class="line">- (BOOL)isTall;</span><br><span class="line">- (BOOL)isRich;</span><br><span class="line">- (BOOL)isHandsome;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">0000 0000 一个字 用后面三个0保存上面三个属性</span><br><span class="line">@interface MJPerson()</span><br><span class="line">&#123;</span><br><span class="line">    // 创建属性</span><br><span class="line">    char _tallRichHansome;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br><span class="line">- (instancetype)init</span><br><span class="line">&#123;</span><br><span class="line">    if (self = [super init]) &#123;</span><br><span class="line">        _tallRichHansome = 0b00000100; //赋初始值 tall = YES  rich = NO handsome = NO</span><br><span class="line">    &#125;</span><br><span class="line">    return self;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>思路GET</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"> 0010 1010	// 假设这是_tallRichHansome值</span><br><span class="line">&amp;0000 0010 = 2	// 想要获取倒数第二的值(rich)</span><br><span class="line">----------</span><br><span class="line"> 0000 0010	// &amp;算法,这样就获取了第二位,十进制的2,那么2不是0所以YES,那么Get方法就可以实现了</span><br><span class="line">// 假设倒数第二是0(rich = NO)那么&amp;下来的结果也是 0</span><br><span class="line">===========================</span><br><span class="line">- (BOOL)isRich</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHansome &amp; 2);	//!一次取反的时候是2变成NO,!再次取反就让(0000 0010 == 2)变成YES,</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>思路SET</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"> 设置的值为YES时</span><br><span class="line"> 0000 0101	// 假设这是_tallRichHansome值</span><br><span class="line">|0000 0010 = 2	// 想要设置倒数第二的值(rich)为YES (|)运算</span><br><span class="line">----------</span><br><span class="line"> 0000 0111</span><br><span class="line"> </span><br><span class="line">  设置的值为NO时</span><br><span class="line"> 0000 0111	// 假设这是_tallRichHansome值</span><br><span class="line">&amp;1111 1101 = 2	// 想要设置倒数第二的值(rich)为NO (&amp;)运算</span><br><span class="line">----------</span><br><span class="line"> 0000 0101</span><br><span class="line">===========================</span><br><span class="line"> - (void)setRich:(BOOL)rich</span><br><span class="line">&#123;</span><br><span class="line">    if (rich) &#123;</span><br><span class="line">        _tallRichHansome |= 2;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHansome &amp;= 0b1111 1101;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>优化实现</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line">// 0000 0111</span><br><span class="line">//&amp;0000 0100</span><br><span class="line">//------</span><br><span class="line">// 0000 0100</span><br><span class="line"></span><br><span class="line">// 掩码，一般用来按位与(&amp;)运算的</span><br><span class="line">//#define MJTallMask 1</span><br><span class="line">//#define MJRichMask 2</span><br><span class="line">//#define MJHandsomeMask 4</span><br><span class="line"></span><br><span class="line">//#define MJTallMask 0b00000001</span><br><span class="line">//#define MJRichMask 0b00000010</span><br><span class="line">//#define MJHandsomeMask 0b00000100</span><br><span class="line"></span><br><span class="line">#define MJTallMask (1&lt;&lt;0)</span><br><span class="line">#define MJRichMask (1&lt;&lt;1)</span><br><span class="line">#define MJHandsomeMask (1&lt;&lt;2)</span><br><span class="line"></span><br><span class="line">@interface MJPerson()</span><br><span class="line">&#123;</span><br><span class="line">    char _tallRichHansome;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation MJPerson</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// 0010 1010</span><br><span class="line">//&amp;1111 1101</span><br><span class="line">//----------</span><br><span class="line">// 0010 1000</span><br><span class="line"></span><br><span class="line">- (instancetype)init</span><br><span class="line">&#123;</span><br><span class="line">    if (self = [super init]) &#123;</span><br><span class="line">        _tallRichHansome = 0b00000100;</span><br><span class="line">    &#125;</span><br><span class="line">    return self;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setTall:(BOOL)tall</span><br><span class="line">&#123;</span><br><span class="line">    if (tall) &#123;</span><br><span class="line">        _tallRichHansome |= MJTallMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHansome &amp;= ~MJTallMask;    //取反 比如 0001 0001 取反之后 1110 1110</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isTall</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHansome &amp; MJTallMask);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setRich:(BOOL)rich</span><br><span class="line">&#123;</span><br><span class="line">    if (rich) &#123;</span><br><span class="line">        _tallRichHansome |= MJRichMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHansome &amp;= ~MJRichMask;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isRich</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHansome &amp; MJRichMask);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setHandsome:(BOOL)handsome</span><br><span class="line">&#123;</span><br><span class="line">    if (handsome) &#123;</span><br><span class="line">        _tallRichHansome |= MJHandsomeMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHansome &amp;= ~MJHandsomeMask;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isHandsome</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHansome &amp; MJHandsomeMask);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">MJPerson *person = [[MJPerson alloc] init];</span><br><span class="line">        person.rich = YES;</span><br><span class="line">        person.tall = NO;</span><br><span class="line">        person.handsome = NO;</span><br><span class="line">        </span><br><span class="line">        NSLog(@&quot;tall:%d rich:%d hansome:%d&quot;, person.isTall, person.isRich, person.isHandsome);</span><br></pre></td></tr></table></figure>
<h4 id="方案2-位域"><a href="#方案2-位域" class="headerlink" title="方案2(位域)"></a>方案2(位域)</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">首先明白一个点</span><br><span class="line">Bool = 0b 0000 0000 (八位二进制)</span><br><span class="line">YES=1,//用一位的二进制来表达的话就是0b1</span><br><span class="line">//而ob1转成Bool之后会发现错误,这个一位的1会变成-1</span><br><span class="line">//因为在ob1的1前面没有别的数字,因此转的时候会把这个1当成特殊符号来使用把前面的7个0一个一个替换成1,因此他是一位的所以转成八位二进制会变成1111 1111 也就是-1</span><br><span class="line">//所以下面取值的时候是这样取值的 !!</span><br><span class="line">return !!_tallRichHandsome.tall; //	不管是-1还是1有值就是YES</span><br><span class="line">//	还有一种解决方案是不用一位的二进制来存储,用二位ob01,这样的加前面不会发生变化,因为1前面多了个0所用都用0来替换掉了前面6位</span><br></pre></td></tr></table></figure>
<p><strong>位域</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">@interface MJPerson()</span><br><span class="line">&#123;</span><br><span class="line">    // 位域</span><br><span class="line">    //	定义结构体,结构体中三个属性char,每一个属性都占用1个字节,但是加上 : 1之后意思是只占1位来表示</span><br><span class="line">    //	谁在上面,谁就在后面 0000 0handsome,rich,tall</span><br><span class="line">    struct &#123;</span><br><span class="line">        char tall : 1;</span><br><span class="line">        char rich : 1;</span><br><span class="line">        char handsome : 1;</span><br><span class="line">    &#125; _tallRichHandsome;	</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<p><strong>继续优化</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">@interface MJPerson()</span><br><span class="line">&#123;</span><br><span class="line">    // 位域</span><br><span class="line">    struct &#123;</span><br><span class="line">        char tall : 1;</span><br><span class="line">        char rich : 1;</span><br><span class="line">        char handsome : 1;</span><br><span class="line">    &#125; _tallRichHandsome;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation MJPerson</span><br><span class="line"></span><br><span class="line">- (void)setTall:(BOOL)tall</span><br><span class="line">&#123;</span><br><span class="line">    _tallRichHandsome.tall = tall;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isTall</span><br><span class="line">&#123;</span><br><span class="line">    return !!_tallRichHandsome.tall;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setRich:(BOOL)rich</span><br><span class="line">&#123;</span><br><span class="line">    _tallRichHandsome.rich = rich;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isRich</span><br><span class="line">&#123;</span><br><span class="line">    return !!_tallRichHandsome.rich;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setHandsome:(BOOL)handsome</span><br><span class="line">&#123;</span><br><span class="line">    _tallRichHandsome.handsome = handsome;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isHandsome</span><br><span class="line">&#123;</span><br><span class="line">    return !!_tallRichHandsome.handsome;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="方案3"><a href="#方案3" class="headerlink" title="方案3"></a>方案3</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 共用体</span><br><span class="line">union Date &#123;</span><br><span class="line">    int year;</span><br><span class="line">    char month;</span><br><span class="line">&#125;;</span><br><span class="line">// 大家公用一块内存</span><br></pre></td></tr></table></figure>
<p><strong>优化</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;MJPerson.h&quot;</span><br><span class="line"></span><br><span class="line">#define MJTallMask (1&lt;&lt;0)</span><br><span class="line">#define MJRichMask (1&lt;&lt;1)</span><br><span class="line">#define MJHandsomeMask (1&lt;&lt;2)</span><br><span class="line">#define MJThinMask (1&lt;&lt;3)</span><br><span class="line"></span><br><span class="line">@interface MJPerson()</span><br><span class="line">&#123;</span><br><span class="line">    union &#123;</span><br><span class="line">        char bits;</span><br><span class="line">        // 这个结构体,其实就一个作用可读性, bits中的存储位置用上面的宏来决定</span><br><span class="line">        struct &#123;</span><br><span class="line">            char tall : 1;</span><br><span class="line">            char rich : 1;</span><br><span class="line">            char handsome : 1;</span><br><span class="line">            char thin : 1;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125; _tallRichHandsome;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">@implementation MJPerson</span><br><span class="line"></span><br><span class="line">- (void)setTall:(BOOL)tall</span><br><span class="line">&#123;</span><br><span class="line">    if (tall) &#123;</span><br><span class="line">        _tallRichHandsome.bits |= MJTallMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHandsome.bits &amp;= ~MJTallMask;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isTall</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHandsome.bits &amp; MJTallMask);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setRich:(BOOL)rich</span><br><span class="line">&#123;</span><br><span class="line">    if (rich) &#123;</span><br><span class="line">        _tallRichHandsome.bits |= MJRichMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHandsome.bits &amp;= ~MJRichMask;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isRich</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHandsome.bits &amp; MJRichMask);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (void)setHandsome:(BOOL)handsome</span><br><span class="line">&#123;</span><br><span class="line">    if (handsome) &#123;</span><br><span class="line">        _tallRichHandsome.bits |= MJHandsomeMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHandsome.bits &amp;= ~MJHandsomeMask;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isHandsome</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHandsome.bits &amp; MJHandsomeMask);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">- (void)setThin:(BOOL)thin</span><br><span class="line">&#123;</span><br><span class="line">    if (thin) &#123;</span><br><span class="line">        _tallRichHandsome.bits |= MJThinMask;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line">        _tallRichHandsome.bits &amp;= ~MJThinMask;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (BOOL)isThin</span><br><span class="line">&#123;</span><br><span class="line">    return !!(_tallRichHandsome.bits &amp; MJThinMask);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>再看看这张图</strong></p>
<p><img src="https://ws2.sinaimg.cn/large/006tKfTcly1ftitovgoupj308x07d74o.jpg" alt=""></p>
<p><img src="https://ws3.sinaimg.cn/large/006tKfTcly1ftixime754j30sn0dddjj.jpg" alt=""></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/06/17/Alipay/" rel="next" title="Alipay">
                <i class="fa fa-chevron-left"></i> Alipay
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/06/20/SwiftProtocol/" rel="prev" title="SwiftProtocol">
                SwiftProtocol <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#Runtime"><span class="nav-number">1.</span> <span class="nav-text">Runtime</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#先了解一下ISA指针-方案1位运算"><span class="nav-number">1.1.</span> <span class="nav-text">先了解一下ISA指针(方案1位运算)</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方案2-位域"><span class="nav-number">2.</span> <span class="nav-text">方案2(位域)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#方案3"><span class="nav-number">3.</span> <span class="nav-text">方案3</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
